/**
 * @(#)com.framework.web.sys.service.UserService
 *
 * Copyright (c) 2014-2018 上海天跃科技有限公司
 *
 * DESC:
 *
 */
package com.framework.web.sys.service;

import com.framework.core.easyui.domain.Tree;
import com.framework.core.util.StringUtil;
import com.framework.web.sys.domain.Organization;
import com.framework.web.sys.domain.Role;
import com.framework.web.sys.repository.OrganizationRepository;
import com.framework.web.sys.repository.specifications.OrganizationSpecs;
import com.framework.web.sys.repository.specifications.RoleSpecs;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fusf
 * @version 1.0  2016/1/8
 */
@Service
public class OrganizationService {

    @Autowired
    private OrganizationRepository organizationRepository;


    /**
     * 分页查找
     *
     * @param organization
     * @param pr
     * @return
     */
    public Page<Organization> getPageList(Organization organization, PageRequest pr) {

        if (StringUtil.isNotEmpty(organization.getId())) {//根据Id设置level
            Organization organization1 = organizationRepository.findOne(organization.getId());
            if (organization1 != null) {
                organization.setLevel(organization1.getLevel());
            }
        }
        Page<Organization> totalList = organizationRepository.findAll(
                OrganizationSpecs.LikeOrgName(organization), pr);
        return totalList;
    }

    /**
     * 新增或者保存
     *
     * @param organization
     * @return
     */
    public Organization save(Organization organization) {
        return organizationRepository.save(organization);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        organizationRepository.delete(id);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Organization findById(String id) {
        return organizationRepository.findOne(id);
    }

    /**
     * 组织机构树查询
     *
     * @param parentId
     * @return
     */
    public List<Organization> findOrgTreeListByParentId(String parentId) {
        List<Organization> treeList = new ArrayList<Organization>();
        if (StringUtil.isNotEmpty(parentId) && !"0".equals(parentId)) {
            treeList = organizationRepository.findByParentIdOrderByLevel(parentId);
        } else {
            treeList = organizationRepository.findRoot();
        }
        return treeList;
    }

    public List<Organization> findAll() {
        return organizationRepository.findAll();
    }

    /*
     * 获取组织树(包含勾选)
     *
     * @param OrganizationList
     * @return
     */
    public List<Tree> findTreeOrganization(List<Organization> OrganizationList) {
        List<Tree> treeList = new ArrayList<Tree>();
        for (Organization Organization : OrganizationList) {//封装成树的格式
            Tree node = new Tree();
            node.setId(Organization.getId());
            node.setText(Organization.getOrgName());
            if (StringUtils.isNotEmpty(Organization.getParentId())) {
                node.setParentId(Organization.getParentId());
            }
            Map<String, String> attributes = new HashMap<String, String>();
            node.setAttributes(attributes);
            treeList.add(node);
        }
        return Tree.getRoot(treeList);
    }

    public String getMaxLevel(String parentId) {
        String level = "";
        if(StringUtil.isEmpty(parentId)) {
             level = organizationRepository.getMaxRootLevel();
            if (StringUtil.isEmpty(level)) {
                level = "0001";
            } else {
                int level_int = Integer.parseInt(level) + 1;
                //补零操作
                level = String.format("%0" + 4 + "d", level_int);
            }
        } else {
            level = organizationRepository.getMaxLevelByParentId(parentId);
            if (level != null && !"".equals(level)) {
                String firstLevel = level.substring(0, level.length() - 4);
                String lastLevel = level.substring(level.length() - 4, level.length());
                int i = Integer.parseInt(lastLevel);
                i += 1;
                lastLevel = i + "";
                while (lastLevel.length() < 4) {
                    lastLevel = "0" + lastLevel;
                }
                level = firstLevel + lastLevel;
            } else {
                Organization parentOrgan = organizationRepository.findOne(parentId);
                level = parentOrgan.getLevel() + "0001";
            }
        }
        return level;
    }

    /**
     * 根据机构名称查询
     * @param orgName
     * @return
     */
    public List<Organization> findByOrgName(String orgName) {
        return organizationRepository.findByOrgName(orgName);
    }


}
